package com.apptravel.web.controller.admin;

import com.apptravel.dao.AreaDAO;
import com.apptravel.dao.Page;
import com.apptravel.dao.TicketDAO;
import com.apptravel.entity.Area;
import com.apptravel.entity.Ticket;
import com.apptravel.util.Constants;
import com.apptravel.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2012-1-15
 * Time: 10:32:24
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/admin/ticket")
public class AdminTicketController extends AdminBaseController {


    private static final String HOME_VIEW = "admin/ticket/home";

    private static final String EDIT_VIEW = "admin/ticket/edit";

    private static final String REDIRECT_VIEW = "/admin/ticket";

    @Autowired
    private TicketDAO ticketDao;

    @Autowired
    private AreaDAO areaDao;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping()
    public String home(ModelMap model, Integer page, Integer rows,Long airlinecomp,String takeofftime,String fromarea,String desarea) {
        Map<String, Object> params = new HashMap<String, Object>();
        if(airlinecomp != null && airlinecomp > 0){
            params.put("airlinecomp",airlinecomp);
            model.put("airlinecomp",airlinecomp);
        }
        if(takeofftime != null && takeofftime.length() > 0){
            params.put("gttakeofftime",takeofftime+" 00:00");
            params.put("lttakeofftime",takeofftime+" 23:59");
            model.put("takeofftime",takeofftime);
        }
        if(fromarea != null && fromarea.length() > 0){

            Map<String,Object> areaParams = new HashMap<String,Object>();
            areaParams.put("areaname","%"+fromarea+"%");
            List<Area> list = areaDao.getListNoPage(areaParams);
            String ids[] = new String[list.size()];
            for(int i=0;i<list.size();i++){
                ids[i] = list.get(i).getId().toString();
            }
            params.put("fromarea",ids);
            areaParams.clear();areaParams = null;
            model.put("fromarea",fromarea);
        }
        if(desarea != null && desarea.length() > 0){
            Map<String,Object> areaParams = new HashMap<String,Object>();
            areaParams.put("areaname","%"+desarea+"%");
            List<Area> list = areaDao.getListNoPage(areaParams);
            String ids[] = new String[list.size()];
            for(int i=0;i<list.size();i++){
                ids[i] = list.get(i).getId().toString();
            }
            params.put("desarea",ids);
            areaParams.clear();areaParams = null;
            model.put("desarea",desarea);
        }
        int pageNo = Constants.DEFAULT_PAGE;
        if (page != null && page > 0)
            pageNo = page;
        int pageSize = Constants.DEFAULT_PAGE_SIZE;
        if (rows != null && rows > 0)
            pageSize = rows;
        Page<Ticket> list = ticketDao.getList(params, pageNo, pageSize);
        model.put("page",list);

        return HOME_VIEW;
    }

    /**
     * 编辑
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(EDIT)
    public String edit(ModelMap model, Long id) {
        if(id != null && id > 0){
            Ticket obj = ticketDao.selectJoinById(id);
            model.put("model",obj);
        }
        return EDIT_VIEW;
    }

    /**
     * 添加.
     *
     * @param ticket
     * @param model
     * @param response
     * @return
     */
    @RequestMapping(ADD)
    public String add(Ticket ticket, ModelMap model, HttpServletResponse response) {
        ticket.setCreatetime(new Date());
        ticketDao.insert(ticket);
        String html = this.makeResponseHtml(0, "");
        JsonUtil.renderHtml(html, response);
        return null;
    }

    /**
     * update
     * @param ticket
     * @param model
     * @param response
     * @return
     */
    @RequestMapping(UPDATE)
    public String update(Ticket ticket,ModelMap model,HttpServletResponse response){
        ticketDao.updateById(ticket);
        
        String html = this.makeResponseHtml(0, "");
        JsonUtil.renderHtml(html, response);
        return null;
    }

    /**
     * 删除.
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(DEL)
    public String del(String ids, HttpServletRequest request,HttpServletResponse response){
        String[] idArray = ids.split(",");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("list", idArray);
        ticketDao.deleteByMap(params);
        String url = request.getContextPath()+REDIRECT_VIEW;
		String html = this.makeResponseHtml(1, url);
		JsonUtil.renderHtml(html, response);
		return null;
    }
}
